package com.ning.weixin.controller;

import com.ning.weixin.dto.IndexDto;
import com.ning.weixin.entity.UserAddress;
import com.ning.weixin.entity.UserInfo;
import com.ning.weixin.service.Oauth2TokenService;
import com.ning.weixin.service.UserAddressService;
import com.ning.weixin.service.UserInfoService;
import com.ning.weixin.utils.R;
import com.ning.weixin.utils.wxUtils.TicketUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import me.chanjar.weixin.common.util.crypto.SHA1;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 用户分享Controller
 *
 * @author Cyan
 * @Date:2018/12/11
 */
@Api(description = "用户分享")
@RestController
@RequestMapping("/njxs/share")
public class ShareController {

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private UserAddressService userAddressService;

    @ApiOperation("分享海报")
    @RequestMapping(value = "/sharestatus", method = RequestMethod.POST)
    public Map<String, Object> shareStatus(@RequestBody IndexDto indexDto) {
        String openId = indexDto.getOpenId();
        //1.获取openId,并得到用户相关信息
        UserInfo userInfo = userInfoService.findUserInfo(openId);
        Integer shareStatus = userInfo.getShareStatus();
        //判断分享的状态是否为0
        if (shareStatus == 0) {
            Integer clockNum = userInfo.getClockNum();
            Integer downClockNum = userInfo.getDownClockNum();
            //2.修改相关信息并保存
            userInfoService.updateShareStatus(
                    new UserInfo(openId, shareStatus, clockNum, downClockNum));
            //用户转发之后获取线下打卡次数14次
            UserAddress userAddress = new UserAddress();
            userAddressService.saveUserAddress(userAddress,openId);
            //3.返回成功页面
            Map<String,Object> map=new HashMap<>();
//            String noncestr = WXUtil.generate();//随机字符串
//            String timestamp = String.valueOf(System.currentTimeMillis() / 1000);//时间戳
//            //4获取url
//            //5、将参数排序并拼接字符串
//            String str = "jsapi_ticket="+ticket+
//                    "&noncestr="+noncestr+"&timestamp="+
//                    timestamp+"&url="+url;
//            //6、将字符串进行sha1加密
//            String signature = SHA1.gen(str);
//            map.put("timestamp",timestamp);
//            map.put("accessToken",accessToken);
//            map.put("ticket",ticket);
//            map.put("noncestr",noncestr);
//            map.put("signature",signature);
            return map;
        }
        return R.error();
    }

}
